<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\command;


use app\common\model\book\Bookread;
use app\common\model\bookdata\Bookdata;
use app\common\model\buy\EgoldLog as EgoldLogModel;
use app\common\model\callback\OrderCallback;
use app\common\model\order\Order;
use app\common\model\promotion\PromotionLink;
use app\common\model\read_uv\ReadUv;
use app\common\model\user\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;


class SyncBookdata extends Command
{
    protected function configure()
    {
        $this->setName('sync_bookdata')
            ->setDescription('更新书籍数据信息');
    }


    protected function execute(Input $input, Output $output)
    {
        try {
            $date = date('Y-m-d',time());
            $start_time = strtotime($date);
            $end_time = strtotime($date)+86400;
            $promotionLink = PromotionLink::select();
            foreach ($promotionLink as $item){
                $data = [];
                $data['date'] = $date;
                $data['promotion_link_id'] = $item->id;
                $data['platform'] = $item->platform;
                $data['admin_id'] = $item->admin_id;
                $data['book_id'] = $item->book_id;
                $user_id_arr = User::where('promotion_link_id',$item->id)->column('id');
//                ->where('create_time','in',[$start_time,$end_time])
                $egold = 0;
                $consumer_users = 0;
                $readnum = 0;
                $number_of_readers = 0;
                for ($i = 0; $i < 50; $i++) {
                    $suffix = '_'.$i;
                    $readnum += Bookread::suffix($suffix)->where('user_id','in',$user_id_arr)->where('create_time','between',[$start_time,$end_time])->where(['book_id'=>$item->book_id])->count();
                    $number_of_readers += Bookread::suffix($suffix)->where('user_id','in',$user_id_arr)->where('create_time','between',[$start_time,$end_time])->where(['book_id'=>$item->book_id])->group('user_id')->select()->count();
                }
                for ($i = 0; $i < 50; $i++) {
                    $suffix = '_'.$i;
                    $egold += EgoldLogModel::suffix($suffix)->where('user_id','in',$user_id_arr)->where('create_time','between',[$start_time,$end_time])->where(['type'=>3,'book_id'=>$item->book_id])->sum('egold');
                    $consumer_users += EgoldLogModel::suffix($suffix)->where('user_id','in',$user_id_arr)->where('create_time','between',[$start_time,$end_time])->where(['type'=>3,'book_id'=>$item->book_id])->group('user_id')->select()->count();
                }
                $data['readnum'] = $readnum;
                $data['number_of_readers'] = $number_of_readers;
                $data['consumer_amount'] = $egold/100;
                $data['consumer_users'] = $consumer_users;
                $data['payment_amount'] = OrderCallback::alias('oc')->leftJoin('order o','oc.order_id=o.id')->where('o.create_time','between',[$start_time,$end_time])->where(['oc.promotion_link_id'=>$item['id'],'o.book_id'=>$item['book_id'],'status'=>1])->sum('money');//充值金额
                $data['payment_users'] = OrderCallback::alias('oc')->leftJoin('order o','oc.order_id=o.id')->where('o.create_time','between',[$start_time,$end_time])->where(['oc.promotion_link_id'=>$item['id'],'o.book_id'=>$item['book_id'],'status'=>1])->group('o.user_id')->select()->count();//充值人数
                $data['payment_num'] = OrderCallback::alias('oc')->leftJoin('order o','oc.order_id=o.id')->where('o.create_time','between',[$start_time,$end_time])->where(['oc.promotion_link_id'=>$item['id'],'o.book_id'=>$item['book_id'],'status'=>1])->count();//充值次数

                $bookdata = Bookdata::where(['date'=>$date,'promotion_link_id'=>$item['id'],'book_id'=>$item->book_id])->findOrEmpty();
                if ($bookdata->isEmpty()){
                    Bookdata::create($data);
                }else{
                    Bookdata::where('id',$bookdata->id)->update($data);
                }
            }
            $output->writeln('sync_bookdata');
            return true;
        } catch (\Exception $e) {
            Log::write('失败,失败原因:' . $e->getMessage());
            return false;
        }
    }

}